package com.service.admin.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.service.admin.admin.dto.LoginUserSession;
import com.service.admin.product.controller.rep.HashRateComboItemRep;
import com.service.admin.product.controller.rep.HashRateComboPageRep;
import com.service.admin.product.controller.req.HashRateComboAddReq;
import com.service.admin.product.controller.req.HashRateComboPageReq;
import com.service.admin.product.controller.req.UpdateNumReq;
import com.service.admin.product.service.ProductHrComboService;
import com.service.admin.support.SessionUtil;
import com.service.core.exception.CommonException;
import com.service.core.rest.response.CommonPageRep;
import com.service.dao.enums.CommonStatusEnums;
import com.service.dao.enums.HrComboEnum;
import com.service.dao.enums.ProductEnums;
import com.service.dao.mapper.ProductHrComboMapper;
import com.service.dao.mapper.ProductHrComboTypeMapper;
import com.service.dao.model.ProductHrCombo;
import com.service.dao.model.ProductHrComboType;
import com.service.utility.modelmapper.ModelMapperUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


@Slf4j
@Service("hashRateComboService")
public class ProductHrComboServiceImpl extends ServiceImpl<ProductHrComboMapper, ProductHrCombo> implements ProductHrComboService {

    @Autowired
    private SessionUtil sessionUtil;

    @Autowired
    private ProductHrComboTypeMapper productHrComboTypeMapper;

//    private HrComboPayType;

    @Override
    public CommonPageRep<HashRateComboPageRep> page(HashRateComboPageReq pageReq) {
        Page<ProductHrCombo> page = PageHelper.startPage(pageReq.getPageNo(), pageReq.getPageSize());
//        LambdaQueryWrapper<ProductHrCombo> wrapper = new QueryWrapper<ProductHrCombo>().lambda();
//        if (!ObjectUtils.isEmpty(pageReq.getComboType())) {
//            wrapper.eq(ProductHrCombo::getComboType, pageReq.getComboType());
//        }
//        if (StringUtils.isNotBlank(pageReq.getComboName())) {
//            wrapper.like(ProductHrCombo::getComboName, pageReq.getComboName());
//        }
//        wrapper.eq(ProductHrCombo::getLanguage, pageReq.getLanguage().getCode());
//        wrapper.eq(ProductHrCombo::getComboStatus, pageReq.getStatus().getCode());
//        wrapper.orderByDesc(ProductHrCombo::getUpdateTime);
//
//        baseMapper.selectList(wrapper);


        List<ProductHrCombo> productHrCombos = baseMapper.page(pageReq.getComboName(),pageReq.getLanguage().getCode(),pageReq.getComboType(),pageReq.getStatus().getCode());

        List<HashRateComboPageRep> resultList = productHrCombos.stream()
                .map(item -> ModelMapperUtil.getStrictModelMapper().map(item, HashRateComboPageRep.class))
                .collect(Collectors.toList());
        return CommonPageRep.buildResultRep(page, resultList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(HashRateComboAddReq addReq) {

        Integer comboType = addReq.getComboType();
        List<ProductHrComboType> productHrComboTypes = productHrComboTypeMapper.selectList(new LambdaQueryWrapper<ProductHrComboType>().eq(ProductHrComboType::getCoinName, addReq.getCoins()));
        int i = 0;
        for (ProductHrComboType productHrComboType : productHrComboTypes) {
            if (productHrComboType.getId().equals(comboType)){
                i++;
                break;
            }
        }
        if (i == 0){
            throw new CommonException("产出货币没有该套餐类型");
        }


        ProductHrCombo combo = ModelMapperUtil.getStrictModelMapper().map(addReq, ProductHrCombo.class);
        LoginUserSession session = sessionUtil.getUser();
        combo.setLanguage(addReq.getComboLanguage().getCode());
        combo.setCoinsId(addReq.getCoins().getCode());
        combo.setCoinsName(addReq.getCoins().getName());
        combo.setBuyNum(0);
        combo.setFakeBuyNum(0);
        combo.setCreateTime(new Date());
        combo.setCreateUserId(session.getId());
        combo.setCreateUserName(session.getNickName());
        combo.setCreateLoginName(session.getLoginName());
        combo.setComboStatus(CommonStatusEnums.ACTIVE.getCode());

        //默认需要质押
        combo.setNeedPledge(ProductEnums.NeedPledge.YES.getCode());
        //默认需要fil gas费
        combo.setNeedGas(ProductEnums.NeedGas.YES.getCode());
        combo.setPledgeNum(addReq.getPledgeNum());
        combo.setGasNum(addReq.getGasNum());


        //新增续约、质押、gas、usdt等参数
        combo.setFromCoinsId(addReq.getFromCoins().getCode());
        combo.setFromCoinsName(addReq.getFromCoins().getName());
        combo.setNeedRenew(addReq.getNeedRenew());
        combo.setRenewalDays(addReq.getRenewalDays());
        combo.setNeedUsdt(addReq.getNeedUsdt());
        combo.setUsdtNum(addReq.getUsdtNum());
        //新增续约、质押、gas、usdt等参数
        baseMapper.insert(combo);
    }

    @Override
    public HashRateComboPageRep detail(Integer id) {
        ProductHrCombo combo = getById(id);
        if (ObjectUtils.isEmpty(combo)) {
            throw new CommonException("获取详情失败,无此套餐");
        }

        return ModelMapperUtil.getStrictModelMapper().map(combo, HashRateComboPageRep.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void modifyCombo(HashRateComboAddReq addReq) {

        Integer comboType = addReq.getComboType();
        List<ProductHrComboType> productHrComboTypes = productHrComboTypeMapper.selectList(new LambdaQueryWrapper<ProductHrComboType>().eq(ProductHrComboType::getCoinName, addReq.getFromCoins()));
        int i = 0;
        for (ProductHrComboType productHrComboType : productHrComboTypes) {
            if (productHrComboType.getId().equals(comboType)){
                i++;
                break;
            }
        }
        if (i == 0){
            throw new CommonException("产出货币没有该套餐类型");
        }





        ProductHrCombo combo = baseMapper.selectById(addReq.getId());
        if (ObjectUtils.isEmpty(combo)) {
            throw new CommonException("修改失败,无此套餐");
        }
        combo = ModelMapperUtil.getStrictModelMapper().map(addReq, ProductHrCombo.class);
        combo.setLanguage(addReq.getComboLanguage().getCode());
        combo.setCoinsId(addReq.getCoins().getCode());
        combo.setCoinsName(addReq.getCoins().getName());
        //新增续约、质押、gas、usdt等参数
        combo.setFromCoinsId(addReq.getFromCoins().getCode());
        combo.setFromCoinsName(addReq.getFromCoins().getName());
        combo.setNeedRenew(addReq.getNeedRenew());
        combo.setRenewalDays(addReq.getRenewalDays());
        combo.setNeedPledge(addReq.getNeedPledge());
        combo.setPledgeNum(addReq.getPledgeNum());
        combo.setNeedGas(addReq.getNeedGas());
        combo.setGasNum(addReq.getGasNum());
        combo.setNeedUsdt(addReq.getNeedUsdt());
        combo.setUsdtNum(addReq.getUsdtNum());
        //新增续约、质押、gas、usdt等参数
        baseMapper.updateById(combo);
    }

    private void assertComboExist(Integer id) {
        ProductHrCombo combo = baseMapper.selectById(id);
        if (ObjectUtils.isEmpty(combo)) {
            throw new CommonException("删除失败,无此套餐");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateComboStatus(Integer id, HrComboEnum status) {
        assertComboExist(id);
        ProductHrCombo combo = new ProductHrCombo();
        combo.setId(id);
        combo.setComboStatus(status.getCode());
        baseMapper.updateById(combo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateComboStatus(Integer id, CommonStatusEnums status) {
        assertComboExist(id);
        ProductHrCombo combo = new ProductHrCombo();
        combo.setId(id);
        combo.setComboStatus(status.getCode());
        baseMapper.updateById(combo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateStockNum(UpdateNumReq numReq) {
        ProductHrCombo combo = baseMapper.selectById(numReq.getId());
        if (ObjectUtils.isEmpty(combo)) {
            throw new CommonException("修改失败,无此套餐");
        }
        combo = new ProductHrCombo();
        combo.setId(numReq.getId());
        combo.setStock(numReq.getNumber());
        baseMapper.updateById(combo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateBuyNum(UpdateNumReq numReq) {
        ProductHrCombo combo = baseMapper.selectById(numReq.getId());
        if (ObjectUtils.isEmpty(combo)) {
            throw new CommonException("修改失败,无此套餐");
        }
        combo = new ProductHrCombo();
        combo.setId(numReq.getId());
        combo.setFakeBuyNum(numReq.getNumber());
        baseMapper.updateById(combo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateMinBuyNum(UpdateNumReq numReq) {
        ProductHrCombo combo = baseMapper.selectById(numReq.getId());
        if (ObjectUtils.isEmpty(combo)) {
            throw new CommonException("修改失败,无此套餐");
        }
        combo = new ProductHrCombo();
        combo.setId(numReq.getId());
        combo.setMinBuyNum(numReq.getNumber());
        baseMapper.updateById(combo);
    }

    @Override
    public void updateProductStockNum(Integer productId, Integer sellNum) {
        ProductHrCombo productHrCombo = getById(productId);

        ProductHrCombo updateHrCombo = new ProductHrCombo();
        updateHrCombo.setId(productHrCombo.getId());
        updateHrCombo.setBuyNum(productHrCombo.getBuyNum() + sellNum);
        updateHrCombo.setFakeBuyNum(productHrCombo.getFakeBuyNum() + sellNum);
        updateHrCombo.setStock(productHrCombo.getStock() - sellNum);
        updateById(updateHrCombo);
    }

    @Override
    public List<HashRateComboItemRep> items() {
        return list(new QueryWrapper<ProductHrCombo>().lambda()
                .eq(ProductHrCombo::getComboStatus, HrComboEnum.PUT_AWAY.getCode())).stream()
                .map(item -> ModelMapperUtil.getStrictModelMapper().map(item, HashRateComboItemRep.class))
                .collect(Collectors.toList());
    }

}
